package C;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.media.MediaLocator;

import Examples.Z_Tools_And_Others.Tools;

import demo.ExampleQueryMsg;
import demo.ExampleResponseMsg;

import jxta.P2PSystem;
import nclab.media.protocol.http.HTTPReceiver;
import nclab.media.protocol.http.HTTPSender;
import net.jxta.exception.PeerGroupException;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.impl.id.UUID.PeerID;
import net.jxta.impl.protocol.ResolverQuery;
import net.jxta.impl.protocol.ResolverResponse;
import net.jxta.peergroup.PeerGroup;
import net.jxta.platform.NetworkManager;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.protocol.ResolverQueryMsg;
import net.jxta.protocol.ResolverResponseMsg;
import net.jxta.resolver.QueryHandler;

public class CPeer implements QueryHandler {

	public static final String peerName = "SPeer";
	public static final String reqDevice = "IPCamera";
	private static PeerID peerId= (PeerID) IDFactory.newPeerID(IDFactory.newPeerGroupID()) ;
	//

	
	//
	

	public static void main(String[] args) {
		
		PipeAdvertisement pipeAdv= null;
		String locatorStr = "http://192.168.1.249/videostream.cgi?user=admin&pwd=123456&resolution=8";
		try {
			//String theSeed = "tcp://"+ InetAddress.getLocalHost().getHostAddress() + ":9710";
			System.out.println(InetAddress.getLocalHost().getHostAddress());
			//String RDVSeed = "tcp://"+ "219.217.239.107" + ":9710";
			String RDVSeed = "http://"+ "219.217.239.107" + ":9710";
			//String RDVSeed = "tcp://"+ InetAddress.getLocalHost().getHostAddress() + ":9710";
			System.out.println("RDVSeed :"+RDVSeed);			 			
			if (!P2PSystem.getInstance().startJxta(peerName,9712,RDVSeed,NetworkManager.ConfigMode.EDGE.toString(), locatorStr)) {
				System.err.println("P2PSystem init failed!");
			}else{
				// register handler
				System.out.println(P2PSystem.getInstance().getOurvodPeerGroup().getResolverService().registerHandler("HandlerName", new CPeer()));
				
				// query//


				//Tools.PopInformationMessage("","Sending query who belongs peer"+P2PSystem.getInstance().getOurvodPeerGroup().getPeerID()+"...");
				

	

				pipeAdv= P2PSystem.getInstance().findDeviceByName(locatorStr);
				if (P2PSystem.getInstance().isSender()){
					HTTPSender hs = new HTTPSender(new MediaLocator(locatorStr), pipeAdv);
					hs.start();
				}
				else{
					HTTPReceiver hr = new HTTPReceiver(pipeAdv);
					hr.start();
				}
			}

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PeerGroupException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public int processQuery(ResolverQueryMsg query) {
		// TODO Auto-generated method stub
		//ResolverResponse response;
		System.out.println("Processing query...");

		System.out.println("Query Body: "+query.getQuery().toString());

		ResolverResponse response = new ResolverResponse();
		response.setHandlerName("HandlerName");
		response.setCredential(null);   // TODO: currently the value is null
		response.setQueryId(query.getQueryId());
		response.setResponse("Set the response body for this message.");
		System.out.println("Sending the response...");
		// Send the request using the Resolver service.
		//P2PSystem.getInstance().getOurvodPeerGroup().getResolverService().sendResponse(query.getSrcPeer().toString(), response);
		//return response;
		return 1;
	}

	@Override
	public void processResponse(ResolverResponseMsg response) {
		// TODO Auto-generated method stub
		ExampleResponseMsg er;
		System.out.println("Processing response...");
		try
		{
			// Extract the message from the Resolver response.
			er = new ExampleResponseMsg(new ByteArrayInputStream(
							(response.getResponse()).getBytes()));
			// Print out the answer given in the response.
			System.out.println("The value of " + er.getBase() + " raised to "
					+ er.getPower() + " is: " + er.getAnswer());
		}
		catch (IOException e){
				// This is not the right type of response message, or
				// the message is improperly formed. Ignore the message,
				// do nothing.
		}
	}
}
